/*
 * Copyright (C) 2014 luke.
 * 
 * This file is part of Silvie.
 * 
 * Silvie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Silvie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Silvie.  If not, see <http://www.gnu.org/licenses/>.
 */

package silvie.gui.filetree;

import java.io.File;
import java.nio.file.Path;

import javax.swing.Icon;

import silvie.gui.SilvieGUI;
import silvie.resources.ResourceFormat;

/**
 * Encapsulates the attributes of a tree node. This class implements Comparable
 * so the nodes can be sorted by filename and type (directory or file).
 * 
 * @author luke
 */
public class FileTreeNode implements Comparable<FileTreeNode> {
	private boolean directory;
	private String name;
	private Path filepath;
	private ResourceFormat format;
	private Icon icon;
	private Icon expandedIcon;

	/**
	 * FileTreeNode constructor.
	 * 
	 * @param file the File from which to create a tree node.
	 */
	public FileTreeNode(File file) {
		this.directory = file.isDirectory();
		this.format = null;
		this.icon = null;
		this.expandedIcon = null;
		if (this.directory) {
			this.icon = SilvieGUI.getCachedIcon(SilvieGUI.DIRECTORY_ICON_PATH);
			this.expandedIcon = SilvieGUI.getCachedIcon(SilvieGUI.EXPANDED_ICON_PATH);
		} else {
			this.format = ResourceFormat.getFormat(file.getAbsolutePath());
			this.icon = this.format == null ? SilvieGUI.getCachedIcon(SilvieGUI.FILE_ICON_PATH) : SilvieGUI.getCachedIcon(SilvieGUI.SUPPORTED_ICON_PATH);
		}
		this.name = file.getName().isEmpty() ? file.getAbsolutePath() : file.getName();
		this.filepath = file.toPath();
	}

	public boolean isDirectory() {
		return this.directory;
	}

	public String getName() {
		return this.name;
	}

	public Path getFilepath() {
		return this.filepath;
	}

	public ResourceFormat getFormat() {
		return this.format;
	}

	public Icon getIcon() {
		return this.icon;
	}

	public Icon getExpandedIcon() {
		return this.expandedIcon;
	}

	@Override
	public int compareTo(FileTreeNode o) {
		// Place directories above files
		if (this.directory && !o.directory) {
			return -1;
		}
		if (!this.directory && o.directory) {
			return 1;
		}
		return this.name.compareTo(o.name);
	}

	// Auto-generated
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.filepath == null) ? 0 : this.filepath.hashCode());
		return result;
	}

	// Auto-generated
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		FileTreeNode other = (FileTreeNode) obj;
		if (this.filepath == null) {
			if (other.filepath != null) {
				return false;
			}
		} else if (!this.filepath.equals(other.filepath)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return this.name;
	}
}
